<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Setting'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                 分享有礼设置
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                   <table class="table" id="table">
                            <tr>
                                 <th>赠送项</th>
                                 <th>赠送值</th>
                                 <th>邀请人获得奖励类型</th>
                                 <th>开关</th>
                            </tr>
                            <tr v-for="(item,index) in form">
                                 <td>{{ item.name }}</td>
                                 <td>
                                      <el-input v-model="item.value" v-if="item.name!='优惠券'"></el-input>

                                        <el-select v-model="item.value" placeholder="请选择" v-if="item.name=='优惠券'">
                                                <el-option
                                                    v-for="item2 in quan"
                                                    :key="item2.id"
                                                    :label="item2.title"
                                                    :value="item2.id"
                                                />
                                        </el-select>

                                 </td>
                                 <td>
                                        <el-radio-group v-model="item.type">
                                            <el-radio value="0">注册成为新用户即送</el-radio>
                                            <el-radio value="1">新用户首次下单即送</el-radio>
                                        </el-radio-group>
                                 </td>
                                 <td>
                                    <el-switch v-model="item.status"></el-switch>
                                 </td>

                            </tr>
                   </table>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="subForm" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref ,watch} from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'
import createAxios from "/@/utils/axios";
const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass
import {ElMessage, ElMessageBox} from "element-plus";
const { t } = useI18n()
const form = ref([{'name':'积分','value':'','status':'0','type':'0'},{'name':'金额','value':'','status':'0','type':'0'},{'name':'优惠券','value':'','status':'0','type':'0'}])
const quan = ref([])

const subForm = () =>{
    return createAxios({
        url: '/admin/mk.Share/setting',
        method: 'post',
        data:{"data":form.value,'act':'post'}
    }).then(res => {
            if(res.data.code == 1){
                ElMessage({'message':'保存成功','type':'success'})
            }else{
                ElMessage({'message':res.data.msg,'type':'error'}) 
            }     
    })
}
const getSettings = () =>{
    return createAxios({
        url: '/admin/mk.Share/setting',
        method: 'post',
        data:{"type":'share','act':'getdata'}
    }).then(res => {
            form.value = res.data.data  
            baTable.form.loading = false
    }) 
}
watch(
    () => baTable.form.operate!, // 监听operate 以此判断编辑或者新增或其他
    (newVal) => {
       
        if(newVal == 'Setting'){
            baTable.form.loading = true
            return createAxios({
                url: '/admin/mk.Coupon/lists',
                method: 'get',
                params:{}
            }).then(res => {
                  quan.value = res.data.list
                  getSettings();   
            })
 
        }

    },
    { deep: true }
)

</script>
<style>
#table .el-input__inner{
    text-align: center;
}
</style>
<style scoped lang="scss">
.table{
     border:1px solid #eee;
     width: 100%;
     border-collapse: collapse; /* 合并边框，消除间隙 */
}
.table th{
    border: 1px solid #eee;
    padding: 10px;
    text-align: left;text-align: center;
}
.table td{
    border: 1px solid #eee;
    padding: 10px;
    text-align: center;
}
.table tr .bg{
    background-color: #f5f7fa;
    font-weight: bold;
    text-align: justify; //
}
</style>
